package com.yuanchu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.order.dao.YoshopRechargeOrderDao;
import com.yuanchu.order.entity.YoshopRechargeOrderEntity;
import com.yuanchu.order.service.YoshopRechargeOrderService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("yoshopRechargeOrderService")
public class YoshopRechargeOrderServiceImpl extends ServiceImpl<YoshopRechargeOrderDao, YoshopRechargeOrderEntity> implements YoshopRechargeOrderService {


    @Override
    public PageResult<YoshopRechargeOrderEntity> queryRechargeOrderList(Integer page, Integer userId) {
        if (userId == null) {
            return null;
        }
        LambdaQueryWrapper<YoshopRechargeOrderEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopRechargeOrderEntity::getUserId, userId);
        // 支付状态(10待支付 20已支付)
        lqw.eq(YoshopRechargeOrderEntity::getPayStatus, 20);
        // 全部
        Page<YoshopRechargeOrderEntity> page1 = new Page<>(page, 15);
        List<YoshopRechargeOrderEntity> records  = this.page(page1, lqw).getRecords();

        records.forEach(order -> order.setPayTime(DateFormatUtils.formatDatetime(order.getPayTime())));

        PageResult<YoshopRechargeOrderEntity> result = new PageResult<>();
        result.setTotal(Math.toIntExact(records.size()));
        result.setCurrentPage(page);
        result.setData(records);
        result.setPerPage(15);
        long total = records.size();
        // 计算总页数，向下取整避免超出实际页数（这里已假设pageInfo.getTotal()返回的是long类型）
        int totalPages = (int) Math.ceil((double) total / 15);
        result.setLastPage(totalPages);

        return result;
    }
}